package com.ajavaer.framework.common.message;

import java.util.List;

public class PageMessage<T> extends Message {
    private Integer page;
    private Integer limit;
    private Integer totalPage;
    private Long count;

    public static <T> PageMessage of(Integer page, Integer limit, Long count, List<T> data) {
        return new PageMessage<>(page, limit, count, data);
    }

    public PageMessage(Integer page, Integer limit, Long count, List<T> data) {
        if (page == null || page <= 0) {
            page = 1;
        }
        if (limit == null || limit <= 0) {
            limit = 10;
        }
        if (count == null || count <= 0) {
            count = 0l;
        }
        this.page = page;
        this.limit = limit;
        this.count = count;
        super.setData(data);
        if (count > 0) {
            this.totalPage = (int) (count / limit + (count % limit == 0 ? 0 : 1));
        } else {
            this.totalPage = 0;
        }
    }

    public Integer getPage() {
        return page;
    }

    public PageMessage<T> setPage(Integer page) {
        this.page = page;
        return this;
    }

    public Integer getLimit() {
        return limit;
    }

    public PageMessage setLimit(Integer limit) {
        this.limit = limit;
        return this;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public PageMessage setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
        return this;
    }

    public Long getCount() {
        return count;
    }

    public PageMessage setCount(Long count) {
        this.count = count;
        return this;
    }
}
